package com.unittec.zk.provider.auth.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.unittec.zk.provider.auth.dto.DepartId;
import com.unittec.zk.provider.auth.dto.SysDepartTree;
import com.unittec.zk.provider.auth.entity.SysDepart;
import com.unittec.zk.sdk.auth.domain.SysDepartDto;

/**
 * @description 处理并查找部门树级数据
 * @author chengjie
 * @date 2019/10/21
 */
public class FindsDepartsChildrenUtil {

    /**
     * 获取 DepartId
     *
     * @param recordList
     * @return
     */
    public static List<DepartId> wrapTreeDataToDepartIdTreeList(List<SysDepart> recordList) {
        List<DepartId> idList = new ArrayList<DepartId>();
        List<SysDepartTree> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysDepart depart = recordList.get(i);
            records.add(new SysDepartTree(depart));
        }
        findChildren(records, idList);
        return idList;
    }


    /**
     * queryTreeList的子方法 ====0===== 该方法是将SysDepart类型的list集合转换成SysDepartTree类型的集合
     */
    public static List<SysDepartTree> wrapTreeDataToTreeList0(List<SysDepart> recordList) {
        List<DepartId> idList = new ArrayList<DepartId>();
        List<SysDepartTree> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysDepart depart = recordList.get(i);
            records.add(new SysDepartTree(depart));
        }
        List<SysDepartTree> tree = findChildren2(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * queryTreeList的子方法 ====1===== 该方法是将SysDepart类型的list集合转换成SysDepartTree类型的集合
     */
    public static List<SysDepartTree> wrapTreeDataToTreeList1(List<SysDepart> recordList) {
        List<DepartId> idList = new ArrayList<DepartId>();
        List<SysDepartTree> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysDepart depart = recordList.get(i);
            records.add(new SysDepartTree(depart));
        }
        List<SysDepartTree> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * queryTreeList的子方法 ====1===== 该方法是将SysDepart类型的list集合转换成SysDepartTree类型的集合
     */
    public static List<SysDepartTree> wrapTreeDataToTreeList4(List<SysDepartDto> recordList) {
        List<DepartId> idList = new ArrayList<DepartId>();
        List<SysDepartTree> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysDepartDto depart = recordList.get(i);
            records.add(new SysDepartTree(depart));
        }
        List<SysDepartTree> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * queryTreeList的子方法 ====1===== 该方法是将SysDepartDto类型的list集合转换成SysDepartTree类型的集合
     */
    public static List<SysDepartTree> wrapTreeDataToTreeList2(List<SysDepartDto> recordList) {
        List<DepartId> idList = new ArrayList<DepartId>();
        List<SysDepartTree> records = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysDepartDto depart = recordList.get(i);
            records.add(new SysDepartTree(depart));
        }
        List<SysDepartTree> tree = findChildren(records, idList);
        setEmptyChildrenAsNull(tree);
        return tree;
    }

    /**
     * queryTreeList的子方法 ====2===== 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<SysDepartTree> findChildren(List<SysDepartTree> recordList, List<DepartId> departIdList) {
        List<SysDepartTree> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysDepartTree branch = recordList.get(i);
            if (StringUtils.isBlank(branch.getParentId())) {
                treeList.add(branch);
                DepartId departIdModel = new DepartId().convert(branch);
                departIdList.add(departIdModel);
            }
        }
        getGrandChildren(treeList, recordList, departIdList);
        return treeList;
    }


    /**
     * queryTreeList的子方法 ====2===== 该方法是找到并封装顶级父类的节点到TreeList集合
     */
    private static List<SysDepartTree> findChildren2(List<SysDepartTree> recordList, List<DepartId> departIdList) {

        Map<String,SysDepartTree> map =new HashMap<>(16);
        for(SysDepartTree dto:recordList){
            map.put(dto.getId(),dto);
        }


        List<SysDepartTree> treeList = new ArrayList<>();
        for (int i = 0; i < recordList.size(); i++) {
            SysDepartTree branch = recordList.get(i);
            if (map.get(branch.getParentId())==null) {
                treeList.add(branch);
                DepartId departIdModel = new DepartId().convert(branch);
                departIdList.add(departIdModel);
            }
        }
        getGrandChildren(treeList, recordList, departIdList);
        return treeList;
    }

    /**
     * queryTreeList的子方法====3==== 该方法是找到顶级父类下的所有子节点集合并封装到TreeList集合
     */
    private static void getGrandChildren(List<SysDepartTree> treeList, List<SysDepartTree> recordList,
        List<DepartId> idList) {
        for (int i = 0; i < treeList.size(); i++) {
            SysDepartTree model = treeList.get(i);
            DepartId idModel = idList.get(i);
            for (int i1 = 0; i1 < recordList.size(); i1++) {
                SysDepartTree m = recordList.get(i1);
                if (m.getParentId() != null && m.getParentId().equals(model.getId())) {
                    model.getChildren().add(m);
                    DepartId dim = new DepartId().convert(m);
                    idModel.getChildren().add(dim);
                }
            }
            getGrandChildren(treeList.get(i).getChildren(), recordList, idList.get(i).getChildren());
        }
    }

    /**
     * queryTreeList的子方法 ====4==== 该方法是将子节点为空的List集合设置为Null值
     */
    private static void setEmptyChildrenAsNull(List<SysDepartTree> treeList) {

        for (int i = 0; i < treeList.size(); i++) {
            SysDepartTree model = treeList.get(i);
            if (model.getChildren().size() == 0) {
                model.setChildren(null);
                model.setLeaf(true);
            } else {
                setEmptyChildrenAsNull(model.getChildren());
                model.setLeaf(false);
            }
        }
    }
}
